﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SalaryManagementSystem.Models;

namespace SalaryManagementSystem.Controllers
{
    public class LoginController : Controller
    {
        //数据库上下文连接对象
        private SalaryManagementBaseEntities1 db = new SalaryManagementBaseEntities1();
        // GET: Login
        public ActionResult Index()
        {//和退出登录衔接，清除用于验证的用户Session信息
            Session["useraccount"] = null;
            Session["username"] = null;
            Session["usertype"] = null;
            Session["allusers"] = null;
            return View();
        }
        //登录方法
        [HttpPost]
        public ActionResult Index(string account,string accountpwd,string user)
        {
            //过滤账号密码，加强程序健壮程度
            account = account.Trim();
            accountpwd = accountpwd.Trim();
            if(account.Length==0 || accountpwd.Length == 0)
            {
                ViewBag.notice = "账号和密码不能为空！";
                return View();
            }
            //判断RadioButton是管理员，执行查询管理员表
            else if(account.Length !=0 && accountpwd.Length !=0 && user=="admin")
            {
                AdminTable adminResult = db.AdminTable.FirstOrDefault(p => p.account == account);
                
                if (adminResult==null)
                {
                    ViewBag.notice = "账号不存在!";
                    return View();
                }
                else if (adminResult.accountpwd != accountpwd)
                {
                    ViewBag.notice = "密码不正确!";
                    return View();
                }
                //将查询到的用户信息用Session存储起来，避免信息显示时再次使用数据库查询
                //降低数据库压力
                Session["userid"] = adminResult.id;
                Session["useraccount"] = account;
                Session["userpwd"] = adminResult.accountpwd;
                Session["usertype"] = adminResult.stafferType;
                Session["usersex"] = adminResult.sex;
                Session["userjob"] = adminResult.job;
                Session["usergetjobtime"] = adminResult.getJobTime;
                Session["username"] = adminResult.stafferName;
                return RedirectToRoute(new { controller = "Admin", action = "Index" });
            }
            //判断RadioButton是普通员工用户，执行查询普通员工表
            else if (account.Length != 0 && accountpwd.Length != 0 && user == "common")
            {
                StafferTable stafferResult = db.StafferTable.FirstOrDefault(p => p.account == account);   
                    if (stafferResult==null)
                    {
                        ViewBag.notice = "账号不存在!";
                        return View();
                    }
                    else if (stafferResult.accountpwd != accountpwd)
                    {
                        ViewBag.notice = "密码不正确!";
                        return View();
                    }
                //将查询到的用户信息用Session存储起来，避免信息显示时再次使用数据库查询
                //降低数据库压力
                Session["useraccount"] = account;
                Session["userpwd"] = stafferResult.accountpwd;
                Session["userid"] = stafferResult.id;
                Session["username"] = stafferResult.stafferName;
                Session["usertype"] = stafferResult.stafferType;
                Session["usersex"] = stafferResult.sex;
                Session["userjob"] = stafferResult.job;
                Session["usergetjobtime"] = stafferResult.getJobTime;
                Session["usersalary"] = stafferResult.salary;
                Session["usersalaryupdatetime"] = stafferResult.updateTime;
                return RedirectToRoute(new { controller = "Staffer", action = "Index" });
            }
            return View();
        }
    }
}